package com.oha.laporrayelchurro.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Enviar el request y el post a la url especificada.
	 * @param req
	 * @param resp
	 * @param url
	 * @throws ServletException
	 * @throws IOException
	 */
	public void goToView(HttpServletRequest req, HttpServletResponse resp, String url) throws ServletException, IOException {
		ServletContext sc = getServletContext();
	    RequestDispatcher rd = sc.getRequestDispatcher(url);
	    rd.forward(req, resp);
	}
	
	/**
	 * Obtiene un valor del request.
	 * @param str
	 * @return
	 */
	public String getParam(HttpServletRequest req, String key){
		String val = req.getParameter(key.trim());
		if(val!=null && val.trim().length()>0){
			return val.trim();
		}else{
			return null;
		}
	}
	
	public Long getLongParam(HttpServletRequest req, String key){
		Long intVal = null;
		String val = req.getParameter(key.trim());
		if(val!=null && val.trim().length()>0){
			try{
				intVal = new Long(val);
			}catch(NumberFormatException nfe){
				intVal = null;
			}	
		}
		return intVal;
	}
}
